package w83b.w83bOperacionesPaso.w83bCitaciones;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import w83b.w83bBeans.W83bCitacionBean;

import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

// TODO: Auto-generated Javadoc
/**
 * The Class W83bTablaAgrupacionesOP.java.
 *
 * @author Deusto Sistemas
 */
public class W83bCargarCitasOutOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		List citas = ((List)contexto.get("w83bListaCitas"));

		JSONObject jsonObject = new JSONObject();
	
		
		W83bCitacionBean cita =null;
		if(citas!=null && citas.size()!=0 )
		{ 
			JSONArray listaCitas=new JSONArray();
			JSONObject citaJSON=new JSONObject();
			for(int i=0;i<citas.size();i++)
			{
				cita=(W83bCitacionBean)citas.get(i);
				citaJSON=new JSONObject();//NOPMD
				citaJSON.put("inicio",cita.getHoraCitacionInicio());
				citaJSON.put("fin",cita.getHoraCitacionFinal());
				listaCitas.put(citaJSON);
				
			}
			jsonObject.put("listaCitas",listaCitas);
			jsonObject.put("hayCitas",new Boolean(true));//NOPMD
			
			
		}else{
			jsonObject.put("hayCitas",new Boolean(false));//NOPMD
			
		}
		
		jsonObject.append("resultado","ok"); 
		
		response.setContentType("text/javascript;charset=iso-8859-1");
		response.setHeader("Pragma", "cache");
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control", "private");
		response.setStatus(HttpServletResponse.SC_OK);
		PrintWriter out = response.getWriter();
		out.print(jsonObject.toString());
		out.flush();
		
		return null;
	}
	
	

}
